
#include <kuai/misc/error.hpp>
#include <kuai/mol/space.hpp>
#include <kuai/sim/studio.hpp>


namespace kuai {

	void SpaceControl::set(const String& key, const String& data)
	{
		if (key == "interval") {
			interval = lexical_cast<size_t>(data);
		}
		else {
			throw UNDEFINED_MEMBER(key);
		}
	}

	String SpaceControl::get(const String& key) const {
		if (key == "interval") {
			return str(interval);
		}
		else {
			throw UNDEFINED_MEMBER(key);
		}
	};


	ControlSet::ControlSet(PhaseSpace& space)
	{
		for (PhaseSpace::iterator
			i = space.begin(); i != space.end(); ++i)
		{
			if (SpaceControl* pe = dynamic_cast<SpaceControl*>(i->second.get())) {
				funcs.push_back(pe);
			}
		}
	}

	void ControlSet::start(kuai::SimulationEnvironment &env, kuai::PhaseSpace &space, kuai::EnergySet &efunc)
	{
		next_x.clear();
		next_x.resize(funcs.size(), 0);

		for (size_t i = 0; i < funcs.size(); ++i) {
			funcs[i]->start(env, space, efunc);
		}
	}
	void ControlSet::act(kuai::SimulationEnvironment &env, size_t step, kuai::PhaseSpace &space, kuai::EnergySet &efunc)
	{
		for (size_t j = 0; j < funcs.size(); ++j) {
			if (next_x[j] < step) {
				funcs[j]->act(env, step, space, efunc);
				next_x[j] += funcs[j]->interval;
			}
		}
	}

	void ControlSet::finish(kuai::SimulationEnvironment &env, size_t step, kuai::PhaseSpace &space, kuai::EnergySet &efunc) {
		for (size_t i = 0; i < funcs.size(); ++i) {
			funcs[i]->finish(env, step, space, efunc);
		}
	}



}
